﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using ZX.Common.Extend;
using ZX.BLL.Extend;
using System.Threading.Tasks;
using System.Data;
using ZX.EHR.Library;
using ZX.CRM.Bussiness;
namespace ZX.WebApp.Crm.Hodometer
{
    public partial class CustomerCheck : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ZX.WebApp.Crm.Hodometer.CustomerCheck));
            if (!this.IsPostBack)
            {
                GetData("");
            }
        }

        protected void gvDataView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#FFFDD7';");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor;");
            }


        }

        //组成查询条件
        protected void BtnQuery_Click(object sender, EventArgs e)
        {
            string str = txtkhname.Text;
            GetData(str);
        }

        protected void GetData(string str)
        {
            DatUser du = (DatUser)Session["User"];
            hdfusername.Value = du.UName;
            ZX.CRM.Library.V_CrmCustomer vcth = new ZX.CRM.Library.V_CrmCustomer();
            BaseBussnies bvcth = new BaseBussnies(vcth);
            IList<ZX.CRM.Library.V_CrmCustomer> vcthl = null;
            BTCrmTeachHodometer bth = new BTCrmTeachHodometer();
            vcthl = bth.getV_CrmCustomer(du.UserID.ToString(),str);
            //if (str == "")
            //{
            //    if (du.UName.ToUpper() == "ADMIN")
            //        vcthl = bvcth.GetEntitys<ZX.CRM.Library.V_CrmCustomer>();
            //    else
            //        vcthl = bvcth.GetEntitys<ZX.CRM.Library.V_CrmCustomer>("AreaId in (select value from fn_Split ((select DeptIds from V_UserEmp where userid='" + du.UserID + "'),','))");
            //}
            //else
            //{
            //    if (du.UName.ToUpper() == "ADMIN")
            //        vcthl = bvcth.GetEntitys<ZX.CRM.Library.V_CrmCustomer>("CustomerName like '%" + str + "%'");
            //    else
            //        vcthl = bvcth.GetEntitys<ZX.CRM.Library.V_CrmCustomer>("AreaId in (select value from fn_Split ((select DeptIds from V_UserEmp where userid='" + du.UserID + "'),',')) and CustomerName like '%" + str + "%'");
            
            //}
            gvDataView.DataSource = vcthl;
            gvDataView.DataBind();

            string ccid=Request.QueryString["CCID"];
            if (ccid != null || ccid != "")
            {
                ZX.CRM.Library.V_CrmTeachHodometer vcc = new ZX.CRM.Library.V_CrmTeachHodometer();
                BaseBussnies bcc = new BaseBussnies(vcc);
                IList<ZX.CRM.Library.V_CrmTeachHodometer> vccl = bcc.GetEntitys<ZX.CRM.Library.V_CrmTeachHodometer>("ThodometerId='" + ccid + "'");
                if (vccl != null)
                {
                    ZX.CRM.Library.V_CrmTeachHodometer vct = vccl[0];
                    ccid = vct.CusID + "|" + vct.CustomerName + "[" + vct.Decisionmaker + "](" + vct.DecisionPosition + ")|" + vct.EXP1+"|"+vct.EXP2;
                }
                else
                    ccid = "";
            }
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Review", "ReviewSelected('"+ccid+"')", true);

        }

        //保存数据
        [AjaxPro.AjaxMethod]
        public string SaveCus(string str)
        {
            string[] strs = str.Split('︾');
            string CCID = strs[0];
            string TEAID=strs[1];
            string DATED = strs[2];
            string TA = strs[3];
            string Cusid=strs[4];
            string addr=strs[5];
            string datt = strs[6];
            string type = "";
            if (Convert.ToDateTime(DATED) < DateTime.Now.AddDays(-1))
            {
                return "该行程日期已过去，不能新增或修改！";
            }
            if (TA.Contains("sw"))//类型：早上，下午，晚上
            {
                type = "A";
            }
            else if (TA.Contains("xw"))
            {
                type = "P";
            }
            else
            {
                type = "N";
            }
            Guid gid = Guid.NewGuid();
            DatUser du = (DatUser)Session["User"];
            ZX.CRM.Library.CrmTeachHodometer cth = new ZX.CRM.Library.CrmTeachHodometer();
            
            if (CCID == "")//新增
            {
                BaseBussnies bcth = new BaseBussnies(cth);
                BTCrmTeachHodometer btcth = new BTCrmTeachHodometer();
                if (btcth.IsExistTeachTime(DATED, TEAID, type))
                {
                    return "该讲师该时间段行程已安排！";
                }
                
                cth.TeachID = new Guid(TEAID);
                cth.HmDate = Convert.ToDateTime(DATED);
                cth.HmType = type;
                if (Cusid == "")
                    return "新增时客户不能为空！";
                
                if (btcth.IsExistCus(Cusid))
                    return "该客户已被安排过！";
                cth.CusID = new Guid(Cusid);
                cth.ThodometerId = gid;
                cth.Addtime = DateTime.Now;
                cth.AddUser = du.UserID;
                cth.EXP1 = addr;
                cth.EXP2 = datt;
                if (bcth.Insert())
                {
                    return gid.ToString()+"|新增成功！";
                }
                else
                    return "新增失败！";
            }
            else
            {
                BaseBussnies bcth = new BaseBussnies(cth);
                IList<ZX.CRM.Library.CrmTeachHodometer> twdl = bcth.GetEntitys<ZX.CRM.Library.CrmTeachHodometer>("ThodometerId='" + CCID + "'");
                if (twdl != null)
                {
                    cth= twdl[0];
                    if (Cusid != "")//修改
                    {
                        cth.CusID = new Guid(Cusid);
                        cth.EXP1 = addr;
                        cth.EXP2 = datt;
                        BaseBussnies blu = new BaseBussnies(cth);
                        if (blu.Update())
                        {
                            return "修改成功！";
                        }
                        else
                        {
                            return "修改失败！"; 
                        }
                    }
                    else
                    {
                        BaseBussnies blu = new BaseBussnies(cth);
                        if (blu.delete())
                        {
                            return "删除成功！";
                        }
                        else
                        {
                            return "删除失败！";
                        }
                    }
                
                }
            }
            return gid.ToString();
        }
    }
}